<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>宿舍信息列表页</title>
<!-- 下载并引入jquery easyUI -->
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.8.8/themes/gray/easyui.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.8.8/themes/icon.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.8.8/themes/color.css">
<script type="text/javascript"
	src="${pageContext.request.contextPath}/jquery-easyui-1.8.8/jquery.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/jquery-easyui-1.8.8/jquery.easyui.min.js"></script>


</head>
<body>
	<table id="dorm_dg" title="宿舍信息列表" class="easyui-datagrid"
		style="width: 1150px; height: 480px"
		url="${param.request.contextPath}/admin/dormindex"
		toolbar="#dorm_toolbar" pagination="true" rownumbers="true"
		fitColumns="true" singleSelect="true">
		<thead>
			<tr>
				<th field="cb" checkbox="true" align="center"></th>
				<th field="dormId" width="50">宿舍编号</th>
				<th field="marName" formatter="dormManagerFormatter" width="50">宿舍管理员姓名</th>

			</tr>
		</thead>
	</table>

	<!-- 工具条组件 -->
	<div id="dorm_toolbar">
		<a href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-add" plain="true" onclick="newDorm()">新增</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-edit" plain="true" onclick="editDorm()">修改</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-remove" plain="true" onclick="destroyDorm()">删除</a>
	</div>

	<!-- 新增和修改表单  start -->
	<div id="dorm_dlg" class="easyui-dialog"
		style="width: 400px; height: 280px; padding: 10px 20px" closed="true"
		buttons="#dorm_dlg-buttons">
		<div class="ftitle">宿舍信息管理</div>
		<form id="dorm_fm" enctype="multipart/form-data" method="post">
			<div class="fitem">
				<label>管理员姓名:</label> <input id="marName" name="dormManager.mgrId"
					class="easyui-combobox"
					data-options="valueField:'mgrId',textField:'marName',
					url:'${param.request.contextPath}/dormManager/listAll'">
			</div>
		</form>
	</div>
	<div id="dorm_dlg-buttons">
		<a href="#" class="easyui-linkbutton" iconCls="icon-ok"
			onclick="saveDorm()">保存</a> <a href="#" class="easyui-linkbutton"
			iconCls="icon-cancel"
			onclick="javascript:$('#dorm_dlg').dialog('close')">取消</a>
	</div>

	<!-- 新增和修改表单 end -->

	<script type="text/javascript">
		function dormManagerFormatter(value, row, index) {
			if (row.dormManager == null) {
				return null;
			}
			return row.dormManager.marName;
		}

		//下拉列表反选的方法
		function loadSuccess1(mgrId) {
			var data = $("#marName").combobox("getData");
			if (data && data.length > 0) {
				if (mgrId == 0) {
					$("#marName").combobox("setValue", data[0]['mgrId']);
				} else {
					$("#marName").combobox("setValue", mgrId);
				}
			}
		}

		// 新增
		function newDorm() {
			$('#dorm_dlg').dialog('open').dialog('setTitle', '新增宿舍信息');
			$('#dorm_fm').form('clear');
			url = '${pageContext.request.contextPath}/dorm/add';
			loadSuccess1(0);

		}

		// 修改
		function editDorm() {
			//$('#dorm_fm').form('clear');
			var row = $('#dorm_dg').datagrid('getSelected');
			if (row) {
				$('#dorm_dlg').dialog('open').dialog('setTitle', '修改宿舍信息');
				$('#dorm_fm').form('load', row);
				url = '${pageContext.request.contextPath}/dorm/edit?dormId='
						+ row.dormId;
				loadSuccess1(row.dormManager.mgrId);
			}
		}

		function saveDorm() {
			$('#dorm_fm').form('submit', {
				url : url,
				onSubmit : function() {
					return $(this).form('validate');
				},
				success : function(result) {
					var result = eval('(' + result + ')');
					if (!result.success) {
						$.messager.show({
							title : 'Error',
							msg : result.errorMsg
						});
					} else {
						$.messager.show({
							title : 'Info',
							msg : result.errorMsg
						});
					}

					$('#dorm_dlg').dialog('close'); // close the dialog
					$('#dorm_dg').datagrid('reload'); // reload the user data
				}
			});
		}

		function destroyDorm() {
			var row = $('#dorm_dg').datagrid('getSelected');
			if (row) {
				$.messager
						.confirm(
								'Confirm',
								'确认删除宿舍信息?',
								function(r) {
									if (r) {
										$
												.post(
														'${pageContext.request.contextPath}/dorm/delete',
														{
															dormId : row.dormId
														},
														function(result) {
															if (result.success) {
																$('#dorm_dg')
																		.datagrid(
																				'reload'); // reload the user data
															} else {
																$.messager
																		.show({ // show error message
																			title : 'Error',
																			msg : result.errorMsg
																		});
															}
														}, 'json');
									}
								});
			}
		}
	</script>
</body>
</html>